package com.pxmeta.app.controller.game;


import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.pxmeta.app.controller.game.request.*;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.order.dto.OrderEvaluateDTO;
import com.pxmeta.order.dto.RequestRefundDTO;
import com.pxmeta.order.dto.SkillOrderCreateDTO;
import com.pxmeta.order.entity.OrderEvaluate;
import com.pxmeta.order.vo.OrderDetailVO;
import com.pxmeta.order.vo.OrderEvaluateVO;
import com.pxmeta.skill.entity.Skill;
import com.pxmeta.party.entity.vo.*;
import com.pxmeta.skill.entity.SkillCertificationForm;
import com.pxmeta.skill.entity.SkillFormItem;
import com.pxmeta.skill.entity.vo.OrderProgressVO;
import com.pxmeta.skill.entity.vo.SkillPriceVO;
import com.pxmeta.skill.service.ISkillCertificationFormService;
import com.pxmeta.skill.service.ISkillFormItemService;
import com.pxmeta.skill.service.ISkillService;
import com.pxmeta.order.service.IUserOrderRecordService;
import com.pxmeta.skill.service.IUserSkillService;
import com.pxmeta.skill.entity.UserSkill;
import com.pxmeta.user.entity.UserTag;
import com.pxmeta.user.service.IUserTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(value = "大神技能",tags = "大神技能")
@RequiredArgsConstructor
@RequestMapping("/skill")
public class SkillController {

    private final IUserSkillService userSkillService;
    private final IUserOrderRecordService userOrderRecordService;
    private final ISkillService skillService;

    private final IUserTagService userTagService;
    private final ISkillFormItemService skillFormItemService;
    private final ISkillCertificationFormService skillCertificationFormService;



    @SaCheckLogin
    @GetMapping("/getHotSkillList")
    @ApiOperation("获取热门技能列表")
    public AjaxResult<List<Skill>> getHotSkillList(){
       List<Skill> skills =  skillService.getHotSkillList().stream().limit(11).collect(Collectors.toList());
       return AjaxResult.success(skills);
    }

    @SaCheckLogin
    @GetMapping("/getAllSkillList")
    @ApiOperation("获取全部技能列表")
    public AjaxResult<List<Skill>> getAllSkillList(){
        List<Skill> skills =  skillService.list();
        return AjaxResult.success(skills);
    }


    @SaCheckLogin
    @GetMapping("/getSkillListByCategory")
    @ApiOperation("根据分类获取技能列表")
    public AjaxResult<List<Skill>> getSkillListByCategory(@RequestParam Integer category){
        Long userId = SecurityUtils.getUserId();
        List<Skill> skills =  skillService.getSkillListByCategory(category, userId);
        return AjaxResult.success(skills);
    }

    @SaCheckLogin
    @GetMapping("/getUserSkillList")
    @ApiOperation("获取用户技能列表")
    public AjaxResult getUserSkillList(){
        Long userId = SecurityUtils.getUserId();
        List<UserSkill> list = userSkillService.getUserSkillList(userId);

        return AjaxResult.success(list);
    }

    @SaCheckLogin
    @GetMapping("/getUserChatSkillList")
    @ApiOperation("获取用户聊天页技能列表")
    public AjaxResult<List<UserSkill>> getUserChatSkillList(@RequestParam String username){
        Long userId = SecurityUtils.getUserId();
        List<UserSkill> list = userSkillService.getUserSkillList(userId,username);
        return AjaxResult.success(list);
    }


    @SaCheckLogin
    @GetMapping("/getSkillPrice")
    @ApiOperation("获取技能价格")
    public AjaxResult<SkillPriceVO> getPrice(@RequestParam Long userSkillId,
                                             @RequestParam Integer count){
        Long userId = SecurityUtils.getUserId();
        SkillPriceVO vo = userSkillService.getPrice(userId,userSkillId,count);
        return AjaxResult.success(vo);
    }

    @SaCheckLogin
    @GetMapping("/getOrderProgress")
    @ApiOperation("查询订单进度")
    public AjaxResult<OrderProgressVO> getOrderProgress(@RequestParam String username){
        Long userId = SecurityUtils.getUserId();
        OrderProgressVO vo = userOrderRecordService.getOrderProgress(userId,username);
        return AjaxResult.success(vo);
    }



    @SaCheckLogin
    @PutMapping("/closeUserSkill")
    @ApiOperation("关闭用户技能")
    public AjaxResult closeUserSkill(@RequestParam Integer skillId){
        Long userId = SecurityUtils.getUserId();
        userSkillService.closeUserSkill(userId, skillId);
        return AjaxResult.success();
    }

    @SaCheckLogin
    @PutMapping("/openUserSkill")
    @ApiOperation("开启用户技能")
    public AjaxResult openUserSkill(@RequestParam Integer skillId){
        Long userId = SecurityUtils.getUserId();
        userSkillService.openUserSkill(userId, skillId);
        return AjaxResult.success();
    }

    @GetMapping("/getItemListByFormId")
    @ApiOperation("根据表单id获取数据列表")
    @SaCheckLogin
    public AjaxResult getItemListByFormId(@RequestParam Integer formId){
        List<SkillFormItem> list = skillFormItemService.getItemListByFormId(formId);
        return AjaxResult.success(list);
    }


//    @SaCheckLogin
//    @GetMapping("/getAllSkillList")
//    @ApiOperation("获取全部技能列表")
//    public AjaxResult<List<SkillCategoryVO>> getAllSkillList(){
//        List<SkillCategoryVO> skills =  skillService.getAllSkillList();
//        return AjaxResult.success(skills);
//    }


    @GetMapping("/getUserList")
    @SaCheckLogin
    @ApiOperation(value = "获取用户列表")
    public AjaxResult<List<UserSkillVO>> getUserList(@RequestParam Integer page,
                                                      @RequestParam Integer size,
                                                      @RequestParam(required = false) Integer skillId,
                                                     @RequestParam(required = false) Integer type){

        String username = SecurityUtils.getUsername();
        List<UserSkillVO> list = userSkillService.getList(username,skillId,page, size,type);
        return AjaxResult.success(list);
    }




    @GetMapping("/getUserSkillDetail")
    @SaCheckLogin
    @ApiOperation(value = "获取用户技能详情")
    public AjaxResult<UserSkillVO> getUserSkillDetail(@RequestParam Long userId,
                                                            @RequestParam Integer skillId){
        Long currentUserId = SecurityUtils.getUserId();
        UserSkillVO detail = userSkillService.getUserSkillDetail(currentUserId, userId, skillId);
        return AjaxResult.success(detail);
    }


    @SaCheckLogin
    @PostMapping("/submitVerify")
    @ApiOperation("提交审核")
    public AjaxResult submit(@RequestBody SkillVerifyDTO dto){
        Long userId = SecurityUtils.getUserId();
        Integer skillId = dto.getSkillId();
        List<SkillVerifyExtraVO> extraList = dto.getExtraList();
        String grade = dto.getGrade();
        String voice = dto.getVoice();
        List<String> imgList = dto.getImgList();
        String introduce = dto.getIntroduce();
        userSkillService.skillVerify(userId, skillId, grade, imgList, introduce,voice, extraList);
        return AjaxResult.success();
    }


    @SaCheckLogin
    @ApiOperation("根据技能id获取申请表单")
    @GetMapping("/getFormBySkillId")
    public AjaxResult getFormByCategory(@RequestParam Integer skillId){
        List<SkillCertificationForm> list = skillCertificationFormService.getBySkillId(skillId);
        return AjaxResult.success(list);
    }



    @SaCheckLogin
    @ApiOperation("获取服务列表")
    @GetMapping("/getServiceList")
    public AjaxResult getServiceList(@RequestParam(required = false) Long userId,
                                     @RequestParam(required = false,defaultValue = "2") Integer category){
        LambdaQueryWrapper<Skill> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Skill::getCategory, category);
        List<Skill> list = skillService.list(wrapper);

        if (null == userId){
            userId = SecurityUtils.getUserId();
        }

        List<UserSkill> userServiceList = userSkillService.getUserList(userId);
        List<SkillVO> finalList = Lists.newLinkedList();
        for (Skill skill : list) {
            SkillVO vo = new SkillVO();
            BeanUtils.copyProperties(skill, vo);
            for (UserSkill userService : userServiceList) {
                if (userService.getSkillId().equals(skill.getId())){
                    vo.setStatus(userService.getStatus());
                }
            }
            finalList.add(vo);
        }
        return AjaxResult.success(finalList);
    }


    @PostMapping("/evaluate")
    @ApiOperation("订单评价")
    @SaCheckLogin
    public AjaxResult scoring(@RequestBody OrderEvaluateDTO evaluateDTO){
        Long userId = SecurityUtils.getUserId();
        userOrderRecordService.evaluate(userId, evaluateDTO);
        return AjaxResult.success();
    }

    @GetMapping("/getEvaluateList")
    @ApiOperation("获取评价列表")
    @SaCheckLogin
    public AjaxResult<List<OrderEvaluateVO>> getEvaluateList(@RequestParam Long targetUserId,
                                      @RequestParam(required = false) Integer skillId,
                                      @RequestParam Integer page,
                                      @RequestParam Integer size){
        List<OrderEvaluateVO> list = userOrderRecordService.getEvaluateList(targetUserId,skillId, page, size);
        return AjaxResult.success(list);
    }


    @GetMapping(value = "/getEvaluateTags")
    @ApiOperation("获取评价标签")
    @SaCheckLogin
    public AjaxResult getEvaluateTags(@RequestParam Long skillId){
        List<UserTag> allTagList = userTagService.getEvaluateTags(skillId);
        return AjaxResult.success(allTagList);
    }

    @PostMapping("/createOrder")
    @ApiOperation("创建订单")
    @SaCheckLogin
    public AjaxResult createOrder(@RequestBody SkillOrderCreateDTO dto){
        Long userSkillId = dto.getUserSkillId();
        Integer count = dto.getCount();
        String partyId = dto.getPartyId();
        Long userId = SecurityUtils.getUserId();
        //创建订单
        Long orderId = userOrderRecordService.createOrder(userId, userSkillId, count, partyId);
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("orderId", orderId);
        return AjaxResult.success(map);
    }


    @PostMapping("/refuseRefund")
    @ApiOperation("拒绝退款")
    @SaCheckLogin
    public AjaxResult refuseRefund(@RequestParam Long orderId){
        Long userId = SecurityUtils.getUserId();
        userOrderRecordService.refuseRefund(userId, orderId);
        return AjaxResult.success();
    }


    @PostMapping("/cancelOrder")
    @ApiOperation("取消订单")
    @SaCheckLogin
    public AjaxResult cancelOrder(@RequestParam Long orderId){
        Long userId = SecurityUtils.getUserId();
        userOrderRecordService.cancelOrder(userId, orderId);
        return AjaxResult.success();
    }

    @PostMapping("/requestRefund")
    @ApiOperation("申请退款")
    @SaCheckLogin
    public AjaxResult requestRefund(@RequestBody RequestRefundDTO dto){
        Long userId = SecurityUtils.getUserId();
        userOrderRecordService.requestRefund(userId, dto);
        return AjaxResult.success();
    }


    @PostMapping("/confirmRefund")
    @ApiOperation("确认退款")
    @SaCheckLogin
    public AjaxResult confirmRefund(@RequestParam Long orderId){
        Long userId = SecurityUtils.getUserId();
        userOrderRecordService.confirmRefund(userId, orderId);
        return AjaxResult.success();
    }


    @PostMapping(value = "/receivingOrder")
    @ApiOperation("接单")
    @SaCheckLogin
    public AjaxResult receivingOrder(@RequestParam Long orderId){
        Long userId = SecurityUtils.getUserId();
        userOrderRecordService.receivingOrder(userId, orderId);
        return AjaxResult.success();
    }


    @PostMapping(value = "/refuseOrder")
    @ApiOperation("拒单")
    @SaCheckLogin
    public AjaxResult refuseOrder(@RequestParam Long orderId){
        Long userId = SecurityUtils.getUserId();
        userOrderRecordService.refuseOrder(userId, orderId);
        return AjaxResult.success();
    }


    @PostMapping(value = "/completeOrder")
    @ApiOperation("完成订单")
    @SaCheckLogin
    public AjaxResult completeOrder(@RequestParam Long orderId){
        Long userId = SecurityUtils.getUserId();
        userOrderRecordService.completeOrder(userId, orderId);
        return AjaxResult.success();
    }

    @GetMapping("/getOrderDetail")
    @ApiOperation("获取订单详情接口")
    @SaCheckLogin
    public AjaxResult<OrderDetailVO> getOrderDetail(@RequestParam Long orderId){
        OrderDetailVO vo = userOrderRecordService.getOrderDetail(orderId);

        return AjaxResult.success(vo);
    }

    @GetMapping("/getOrderList")
    @SaCheckLogin
    @ApiOperation("获取订单列表")
    public AjaxResult<List<OrderDetailVO>> getOrderList(@RequestParam Integer page,
                                                        @RequestParam Integer size,
                                                        @RequestParam Integer type){
        if (size>20){
            return AjaxResult.error("分页长度过长！");
        }

        Long userId = SecurityUtils.getUserId();
        List<OrderDetailVO> list = userOrderRecordService.getOrderList(userId,type, page, size);

        return AjaxResult.success(list);
    }




    @PostMapping("/charging")
    @ApiOperation("扣费")
    @SaCheckLogin
    public AjaxResult charging(@RequestBody UserOrderChargingDTO chargingDTO){
        Long userId = SecurityUtils.getUserId();
        userOrderRecordService.charging(userId,chargingDTO.getRecordId(), chargingDTO.getServicePriceId());
        return AjaxResult.success();
    }
}
